﻿package model.poker;

/**
 * 
 * 扑克服务进程
 * 用于斗地主
 * @author qykings
 *
 */
import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.jboss.netty.channel.Channel;

import config.poker.PokerConfig;

import protocol.poker.Poker_protocol;

import queue.KSocketQueueCenter;

import model.message.MessageVo;
import util.KIntChange;
import util.KPrintInfo;

public class PokerServer {
	private static PokerServer	pServer;
	
	// 报名的队列
	private ArrayList<Channel>	sign_arr		= new ArrayList<Channel>();
	
	private ArrayList<Table>	tablesArrayList	= new ArrayList<Table>();
	
	static public PokerServer getInstance() {
		if (pServer == null) {
			pServer = new PokerServer();
		}
		return pServer;
	}
	
	public void MessageHandle(MessageVo msg) throws IOException {
		Map contentJsonObject = (HashMap) msg.getContent();
		int operate = KIntChange.intChange(contentJsonObject.get("operate"));
		
		if (operate == PokerConfig.getInstance().getPoker_sign_up()) {
			// 报名
			sign_up(msg);
		} else if (operate == PokerConfig.getInstance().getPoker_ready()) {
			// 准备
			ready(msg);
		} else if (operate == PokerConfig.getInstance().getPoker_RobLead_Times()) {
			// 抢地主
			RobLead_Times_Handle(msg);
		} else if (operate == PokerConfig.getInstance().getPoker_Play_Card_send()) {
			// 出牌
			
		} else if (operate == PokerConfig.getInstance().getPoker_Play_Card_pass()) {
			// 过
		}
		
	}
	
	/**
	 * 有人报名
	 * 
	 * @param msg
	 */
	public void sign_up(MessageVo msg) {
		Channel channel = msg.getChannel();
		if (!obServerPeoPleIsExit(channel)) {
			sign_arr.add(msg.getChannel());
			obServerPeoPleNumber();
		}
	}
	
	private boolean obServerPeoPleIsExit(Channel channel) {
		for (int i = 0; i < sign_arr.size(); i++) {
			if (sign_arr.get(i) == channel) {
				return true;
			}
		}
		return false;
	}
	
	// 准备
	public void ready(MessageVo msg) throws IOException {
		for (int i = 0; i < tablesArrayList.size(); i++) {
			if (tablesArrayList.get(i).haveThisPeople(msg.getChannel())) {
				tablesArrayList.get(i).readyPeople(msg.getChannel());
			}
		}
	}
	
	// 够三个人了可以开始了
	private void obServerPeoPleNumber() {
		if (sign_arr.size() >= 3) {
			Table table = new Table();
			for (int i = 0; i < 3; i++) {
				table.getPlayerList().add(sign_arr.get(0));
				sign_arr.remove(0);
				KPrintInfo.println(table);
				KPrintInfo.println(sign_arr);
			}
			tablesArrayList.add(table);
			table.setTableID(tablesArrayList.size());
			table.start();
		}
	}
	
	// 抢地主倍数
	public void RobLead_Times_Handle(MessageVo msg) throws IOException {
		for (int i = 0; i < tablesArrayList.size(); i++) {
			if (tablesArrayList.get(i).haveThisPeople(msg.getChannel())) {
				tablesArrayList.get(i).handleLeadTimes(msg);
			}
		}
	}
	
	// 出牌
	public void sendCard(MessageVo msg) {
		for (int i = 0; i < tablesArrayList.size(); i++) {
			if (tablesArrayList.get(i).haveThisPeople(msg.getChannel())) {
				tablesArrayList.get(i).sendCard(msg);
			}
		}
	}
	
	public void SomeOneExit(Channel channel) {
		try {
			if (!obServerPeoPleIsExit(channel)) {
				for (int i = 0; i < tablesArrayList.size(); i++) {
					if (tablesArrayList.get(i).haveThisPeople(channel)) {
						Poker_protocol poker_protocol = new Poker_protocol();
						poker_protocol.setOperate(PokerConfig.getInstance().getSomeOneExit());
						tablesArrayList.get(i).tableBroadMsg(poker_protocol);
						tablesArrayList.get(i).destory();
						tablesArrayList.remove(i);
					}
				}
			}
		} catch (Exception e) {
			System.out.println(e);
		}
		// tablesArrayList
	}
	
	public void destory() {
		
	}
	
	// //end class
}
